using System;
using Slf;
using Slf.Factories;
using Slf.Resolvers;
using Microsoft.VisualStudio.TestTools.UnitTesting;


namespace SLF.Test.Logger_Service
{
  /// <summary>
  /// 
  /// </summary>
  [TestClass]
  public class LoggerServiceTest
  {
    [TestMethod]
    public void Logger_Service_Should_Provide_App_Config_Resolver_Per_Default()
    {
      //TODO add Silverlight test project and make sure service is initialized as expected
      LoggerService.Reset();
      Assert.IsInstanceOfType(LoggerService.FactoryResolver, typeof (AppConfigFactoryResolver));
    }


    /// <summary>
    /// 
    /// </summary>
    [TestMethod]
    public void Requesting_A_Logger_By_Name_Should_Return_A_Logger_Instance_With_The_Given_Name()
    {
      LoggerService.FactoryResolver = new SimpleFactoryResolver(new ConsoleLoggerFactory());

      ILogger logger = LoggerService.GetLogger("foo");
      Assert.AreEqual("foo", logger.Name);

      logger = LoggerService.GetLogger("DifferentCases");
      Assert.AreEqual("DifferentCases", logger.Name);

      logger = LoggerService.GetLogger();
      Assert.AreEqual(LoggerService.DefaultLoggerName, logger.Name);

      logger = LoggerService.GetLogger(null);
      Assert.AreEqual(LoggerService.DefaultLoggerName, logger.Name);
    }


    [TestMethod]
    public void If_Resolver_Returns_A_Null_Instance_LoggerService_Should_Fall_Back_To_NullLogger()
    {
      LoggerService.FactoryResolver = new NullResolver();
      Assert.IsNull(LoggerService.FactoryResolver.GetFactory("xx"));

      Assert.IsInstanceOfType(LoggerService.GetLogger("xx"), typeof(NullLogger));
    }


    private class NullResolver : IFactoryResolver
    {
      public ILoggerFactory GetFactory(string loggerName)
      {
        return null;
      }
    }


    /// <summary>
    /// 
    /// </summary>
    [TestMethod]
    public void Assigning_Null_For_Resolver_Falls_Back_To_NullLogger()
    {
      Assert.IsNotNull(LoggerService.FactoryResolver);
      LoggerService.FactoryResolver = null;
      Assert.IsNotNull(LoggerService.FactoryResolver);

      Assert.IsInstanceOfType(LoggerService.FactoryResolver.GetFactory("xx"), typeof(NullLoggerFactory));
    }


    [TestMethod]
    public void Setting_Null_As_Default_Logger_Should_Fall_Back_To_Null_Logger()
    {
      var logger = new ConsoleLogger();
      LoggerService.SetLogger(logger);
      Assert.AreSame(logger, LoggerService.GetLogger("xx"));
      LoggerService.SetLogger(null);
      Assert.IsInstanceOfType(LoggerService.GetLogger("xx"), typeof(NullLogger));
    }
  }
}
